﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.UI.WebControls;

namespace QuanLyVanBanBUS
{
    public class PhamViBUS
    {
        public void themPhamVi(PhamVi pvDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            storeDB.PhamVis.InsertOnSubmit(pvDTO);

            storeDB.SubmitChanges();
        }

        public void suaPhamVi(PhamVi pvDTO, string maPhamVi)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.PhamVis.Where(q => q.MaPhamVi == maPhamVi).First();
            query.TenPhamVi = pvDTO.TenPhamVi;

            storeDB.SubmitChanges();
        }

        public void xoaPhamVi(GridView gridView)
        {
            //Lay MaPhamVi duoc chon tu CheckBox
            string temp = string.Empty;

            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox _checkBox = (CheckBox)(gridView.Rows[i].Cells[0].FindControl("chkMaPhamVi"));

                if (_checkBox.Checked == true)
                {
                    string maPhamViCanXoa = gridView.DataKeys[i].Value.ToString();

                    temp += maPhamViCanXoa + ",";
                }
            }

            //Kiem tra co check vao CheckBox khong?
            if (temp.Length - 1 > 0)
            {
                temp = temp.Substring(0, temp.Length - 1);
            }
            else
            {
                //Response.Write("<script>alert('Xin vui lòng chọn Sản phẩm cần xóa')</script>");

                return;
            }

            //Xoa
            QLVBDataContext storeDB = new QLVBDataContext();

            //Them cac MaPhamVi duoc chon vao mang
            string[] m_temp = new string[] { temp };

            while (temp.Length > 0)
            {
                //Lay maTemp de lam dieu kien so sanh. Lay tu sau lay toi cua Mang
                string maTemp = temp.Substring(temp.LastIndexOf(",") + 1, temp.Length - (temp.LastIndexOf(",") + 1));

                var query = storeDB.PhamVis.Where(q => q.MaPhamVi == maTemp).First();

                storeDB.PhamVis.DeleteOnSubmit(query);

                storeDB.SubmitChanges();

                //Kiem tra dieu kien co con trong vong lap While khong?
                if ((temp.Length - (maTemp.Length + 1)) > 0)
                {
                    temp = temp.Substring(0, temp.Length - (maTemp.Length + 1));
                }
                else
                    break;
            }

            gridView.DataSource = layDanhSachPhamVi();
            gridView.DataBind();
        }

        public IList<PhamVi> layDanhSachPhamVi()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var lstPhamVi = storeDB.PhamVis.ToList();

            return lstPhamVi;
        }

        public Boolean kiemtraTonTaiMaPhamVi(string maPhamVi)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.PhamVis.Where(q => q.MaPhamVi == maPhamVi).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraTonTaiTenPhamVi(string tenPhamVi)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.PhamVis.Where(q => q.TenPhamVi == tenPhamVi).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public string layMaPhamVi(string maPhamVi)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var _maPhamVi = storeDB.PhamVis.Where(m => m.MaPhamVi == maPhamVi).Select(m => m.MaPhamVi).First();

            return _maPhamVi;
        }

        public string layTenPhamVi(string maPhamVi)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var tenPhamVi = storeDB.PhamVis.Where(m => m.MaPhamVi == maPhamVi).Select(m => m.TenPhamVi).First();

            return tenPhamVi;
        }

        public string layMaPhamViTheoSoVanBanMaPhong(string soVanBan, string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            int namHienTai = DateTime.Now.Date.Year;

            int namMax = Convert.ToInt32((from vb in storeDB.VanBans
                                          join nd in storeDB.NguoiDungs on vb.MaNguoiSoanThao equals nd.MaNguoiDung
                                          where vb.MaNguoiKiemSoat == null && vb.NgayKiemSoat == null && vb.NgayXoaHienThi == null && vb.MaTrangThai == "CD" && nd.MaPhong == maPhong && vb.SoVanBan == soVanBan
                                          select vb.MaVanBan).Max().Substring(0, 4));

            var query = (from vb in storeDB.VanBans
                         join nd in storeDB.NguoiDungs on vb.MaNguoiSoanThao equals nd.MaNguoiDung
                         where vb.MaNguoiKiemSoat == null && vb.NgayKiemSoat == null && vb.NgayXoaHienThi == null && vb.MaTrangThai == "CD" && nd.MaPhong == maPhong && vb.SoVanBan == soVanBan && namHienTai == namMax
                         select vb.MaPhamVi).ToList();

            return query.Last();
        }
    }
}
